<!-- extend from base layout -->
{% extends "layout.html" %}

{% block extra_css %}

{% endblock %}

{% block content %}
    <div class="container-fluid">
    <ol class="breadcrumb">
        <li><a href="/">后台管理</a></li>
        <li><a href="javascript:void(0);" onclick="$('#menu-user').trigger('click');">会员管理</a></li>
        <li class="active">会员列表</li>
    </ol>

    <form class="form-inline">
        <div class="form-group{% if form.user_id.errors %} has-error{% endif %}">
            {{ form.user_id.label(class="sr-only") }}
            {{ form.user_id(class="form-control", placeholder="用户ID") }}
            {% for error in form.user_id.errors %}
                <span class="help-block">{{ error }}</span>
            {% endfor %}
        </div>
        <div class="form-group{% if form.user_name.errors %} has-error{% endif %}">
            {{ form.user_name.label(class="sr-only") }}
            {{ form.user_name(class="form-control", placeholder="用户名称") }}
            {% for error in form.user_name.errors %}
                <span class="help-block">{{ error }}</span>
            {% endfor %}
        </div>
        <div class="form-group{% if form.start_time.errors %} has-error{% endif %}">
            {{ form.start_time.label(class="sr-only") }}
            {{ form.start_time(class="form-control", placeholder="start_time", type='date') }}
            {% for error in form.start_time.errors %}
                <span class="help-block">{{ error }}</span>
            {% endfor %}
        </div>
        <div class="form-group{% if form.end_time.errors %} has-error{% endif %}">
            {{ form.end_time.label(class="sr-only") }}
            {{ form.end_time(class="form-control", placeholder="end_time", type='date') }}
            {% for error in form.end_time.errors %}
                <span class="help-block">{{ error }}</span>
            {% endfor %}
        </div>
        <div class="form-group{% if form.status_active.errors %} has-error{% endif %}">
            {{ form.status_active.label(class="sr-only") }}
            {{ form.status_active(class="form-control", placeholder="激活状态", data_width="fit", data_header="选择状态") }}
            {% for error in form.status_active.errors %}
                <span class="help-block">{{ error }}</span>
            {% endfor %}
        </div>
        <div class="form-group{% if form.status_lock.errors %} has-error{% endif %}">
            {{ form.status_lock.label(class="sr-only") }}
            {{ form.status_lock(class="form-control", placeholder="锁定状态", data_width="fit", data_header="选择状态") }}
            {% for error in form.status_lock.errors %}
                <span class="help-block">{{ error }}</span>
            {% endfor %}
        </div>
        <button type="submit" class="btn btn-primary" name="op" value="0">Search</button>
        <button type="submit" class="btn btn-primary" name="op" value="1">Export</button>
    </form>

    <hr/>

    <div class="table-responsive">
        {# <table class="table table-striped"> #}
        <table class="table table-hover">
            <thead>
            <tr>
                <th>用户ID</th>
                <th>用户名称</th>
                <th>等级</th>
                <th>手机号码</th>
                <th>推荐人</th>
                <th>钱包余额</th>
                <th>数字货币</th>
{#                <th>积分</th>#}
                <th>奖金</th>
                <th>排单币</th>
                <th>激活状态</th>
                <th>锁定状态</th>
                <th>创建时间</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            {% for (user, user_profile_c, user_profile_p, wallet, bit_coin, score, bonus, scheduling) in pagination.items %}
            <tr class="text-muted">
                <td>{{ user.id }}</td>
                <td><a href="{{ url_for('user.admin_login', user_id=user.id) }}" rel="tooltip" title="登录" target="_blank">{{ user_profile_c.nickname }}</a></td>
                <td>{{ user_profile_c.type_level | type_level }}</td>
                <td>{{ user_profile_c.phone }}</td>
                {% if user_profile_p %}
                <td><a href="{{ url_for('user.admin_login', user_id=user_profile_p.user_id) }}" rel="tooltip" title="登录" target="_blank">{{ user_profile_p.nickname }}</a></td>
                {% else %}
                <td></td>
                {% endif %}
                <td>{{ wallet.amount_current | default('0') }}</td>
                <td>{{ bit_coin.amount | default('0') }}</td>
{#                <td>{{ score.amount | default('0') }}</td>#}
                <td>{{ bonus.amount | default('0') }}</td>
                <td>{{ scheduling.amount | default('0') }}</td>
                <td>{{ user.status_active | status_active }}</td>
                <td>{{ user.status_lock | status_lock }}</td>
                <td>{{ moment(user.create_time).format('YYYY-MM-DD HH:mm:ss') }}</td>
                <td>
                    <a href="{{ url_for('active.add', user_id=user.id, next=request.path) }}" rel="tooltip" title="激活码">
                        <span class="glyphicon glyphicon-qrcode"></span>
                    </a>
                    <a href="{{ url_for('scheduling.add', user_id=user.id, next=request.path) }}" rel="tooltip" title="排单币">
                        <span class="glyphicon glyphicon-euro"></span>
                    </a>
                    <a href="{{ url_for('user.profile', user_id=user.id, next=request.path) }}" rel="tooltip" title="编辑">
                        <span class="glyphicon glyphicon-pencil"></span>
                    </a>
                    <a href="{{ url_for('user.config', user_id=user.id, next=request.path) }}" rel="tooltip" title="配置">
                        <span class="glyphicon glyphicon-cog"></span>
                    </a>
                    {% if user.status_lock == STATUS_LOCK_OK | int %}
                    <a href="javascript:void(0);" onclick="user_unlock({{ user.id }});" rel="tooltip" title="解锁">
                        <span class="fa fa-unlock-alt"></span>
                    </a>
                    {% else %}
                    <a href="javascript:void(0);" onclick="user_lock({{ user.id }});" rel="tooltip" title="锁定">
                        <span class="glyphicon glyphicon-lock"></span>
                    </a>
                    {% endif %}
                    <a href="javascript:void(0);" onclick="user_delete({{ user.id }});" rel="tooltip" title="删除">
                        <span class="glyphicon glyphicon-trash"></span>
                    </a>
                </td>
            </tr>
            {% endfor %}
            </tbody>
        </table>
        {# 翻页 #}
        {% from "macros.html" import render_pagination %}
        {{ render_pagination(pagination, 'user.lists') }}

    </div>
    </div>
{% endblock %}

{% block extra_js %}
<script>
    // 初始化赋值
    var status_active = $('#status_active');
    status_active.selectpicker('val', '{{ form.status_active.data }}');
    // 处理选项修改
    status_active.on('changed.bs.select', function (event, clickedIndex, newValue, oldValue) {
        // console.log($('#status_active').val());
        // console.log(event);
        // console.log(clickedIndex);
        // console.log(newValue);
        // console.log(oldValue);
    });

    var status_lock = $('#status_lock');
    status_lock.selectpicker('val', '{{ form.status_lock.data }}');
    // 处理选项修改
    status_lock.on('changed.bs.select', function (event, clickedIndex, newValue, oldValue) {
        // console.log($('#status_lock').val());
        // console.log(event);
        // console.log(clickedIndex);
        // console.log(newValue);
        // console.log(oldValue);
    });

    // 用户锁定
    function user_lock(user_id){
        if(confirm("用户锁定操作，是否确认?"))
        {
            // console.log(user_id);
            $.getJSON('{{ url_for('user.ajax_lock') }}',
            {
                user_id: user_id
            }, function (result) {
                if(result.hasOwnProperty('error')){
                    alert(result.error);
                }else {
                    alert(result.success);
                    window.location.reload();
                }
            });
            return false;
        }
    }

    // 用户解锁
    function user_unlock(user_id){
        if(confirm("用户解锁操作，是否确认?"))
        {
            // console.log(user_id);
            $.getJSON('{{ url_for('user.ajax_unlock') }}',
            {
                user_id: user_id
            }, function (result) {
                if(result.hasOwnProperty('error')){
                    alert(result.error);
                }else {
                    alert(result.success);
                    window.location.reload();
                }
            });
            return false;
        }
    }

    // 用户删除
    function user_delete(user_id){
        if(confirm("删除数据不能恢复，是否确认删除?"))
        {
            // console.log(user_id);
            $.getJSON('{{ url_for('user.ajax_delete') }}',
            {
                user_id: user_id
            }, function (result) {
                if(result.hasOwnProperty('error')){
                    alert(result.error);
                }else {
                    alert(result.success);
                    window.location.reload();
                }
            });
            return false;
        }
    }
</script>
{% endblock %}
